#!/usr/bin/env bash
# 结构化shell例子
# 测试 if - else if - else  。 &&，|| 表示and，or运算
testuser=wangxi
#
#if test $testuser   # test代表后面表达式是否返回0, 当前语句测试testuser是否有值
if [ $testuser ]   # [] 与test用法一直 当前语句测试testuser是否有值
then
    echo "The bash files for user $testuser are:"
    # ls /Users/$testuser
    echo
elif ls -d /Users/$testuser
then
    echo "The user $testuser does not exist on this system."
    echo "However, $testuser has a directory."
else
    echo "The user $testuser does not exist on this system."
    echo
fi

# 文件比较，测试文件和目录的状态  -d比较目录
jump_directory=/home/arthur #
if [ -d $jump_directory ]
then
       echo "The $jump_directory directory exists"
       cd $jump_directory
       ls
    else
       echo "The $jump_directory directory does not exist"
fi

# -e 比较可用于文件和目录。要确定指定对象为文件，必须用-f比较
item_name=$HOME
echo
echo "The item being checked: $item_name" echo
#
if [ -e $item_name ]
then #Item does exist
    echo "The item, $item_name, does exist."
    echo "But is it a file?"
    echo
    #
    if [ -f $item_name ]
    then #Item is a file
        echo "Yes, $item_name is a file." #
    else #Item is not a file
        echo "No, $item_name is not a file."
    fi #
else #Item does not exist
    echo "The item, $item_name, does not exist." echo "Nothing to update"
fi